"""
主界面模块
实现用户交互界面
"""

import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, 
                             QHBoxLayout, QPushButton, QLabel, QProgressBar, 
                             QLineEdit, QFileDialog, QMessageBox, QGroupBox,
                             QComboBox, QTabWidget, QTextEdit, QSpinBox, QDoubleSpinBox)
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QFont, QColor, QPalette, QPixmap
import threading
from main_controller import get_main_controller
from system_state import SystemState
from motion_generator import DanceMode
from robot_model_viewer import RobotModelViewer


class MainUI(QMainWindow):
    """
    主界面类
    实现用户交互界面
    """
    
    def __init__(self):
        """初始化主界面"""
        super().__init__()
        self.controller = get_main_controller()
        
        # 界面状态
        self.updating = False
        
        # 初始化界面
        self.init_ui()
        
        # 初始化机器人模型查看器
        self.robot_model_viewer = RobotModelViewer(self.model_display)
        
        # 初始化定时器
        self.init_timers()
        
        # 初始化系统
        self.initialize_system()
        
    def init_ui(self):
        """初始化用户界面"""
        self.setWindowTitle('机器人舞蹈控制系统')
        self.setGeometry(100, 100, 900, 700)
        
        # 设置应用程序样式
        self.setStyleSheet("""
            QMainWindow {
                background-color: #2b2b2b;
            }
            QGroupBox {
                font-weight: bold;
                border: 2px solid #3daee9;
                border-radius: 8px;
                margin-top: 1ex;
                padding-top: 10px;
                background-color: #31363b;
                color: #eff0f1;
            }
            QGroupBox::title {
                subcontrol-origin: margin;
                subcontrol-position: top center;
                padding: 0 5px;
                background-color: #3daee9;
                color: white;
                border-radius: 3px;
            }
            QLabel {
                color: #eff0f1;
            }
            QPushButton {
                background-color: #3daee9;
                border: none;
                color: white;
                padding: 8px 16px;
                border-radius: 4px;
                font-weight: bold;
            }
            QPushButton:hover {
                background-color: #4db8ff;
            }
            QPushButton:pressed {
                background-color: #2e86b3;
            }
            QPushButton:disabled {
                background-color: #7a7a7a;
                color: #aaaaaa;
            }
            QTabWidget::pane {
                border: 1px solid #3daee9;
                border-radius: 5px;
                background-color: #31363b;
            }
            QTabBar::tab {
                background-color: #232629;
                color: #aaaaaa;
                padding: 10px;
                border: 1px solid #3daee9;
                border-bottom: none;
                border-top-left-radius: 5px;
                border-top-right-radius: 5px;
            }
            QTabBar::tab:selected {
                background-color: #3daee9;
                color: white;
                font-weight: bold;
            }
            QTabBar::tab:hover:!selected {
                background-color: #336699;
                color: white;
            }
            QLineEdit {
                padding: 5px;
                border: 1px solid #3daee9;
                border-radius: 4px;
                background-color: #232629;
                color: #eff0f1;
            }
            QComboBox {
                padding: 5px;
                border: 1px solid #3daee9;
                border-radius: 4px;
                background-color: #232629;
                color: #eff0f1;
            }
            QComboBox::drop-down {
                border: none;
            }
            QSpinBox, QDoubleSpinBox {
                padding: 5px;
                border: 1px solid #3daee9;
                border-radius: 4px;
                background-color: #232629;
                color: #eff0f1;
            }
            QTextEdit {
                border: 1px solid #3daee9;
                border-radius: 4px;
                background-color: #232629;
                color: #eff0f1;
            }
            QProgressBar {
                border: 1px solid #3daee9;
                border-radius: 4px;
                text-align: center;
                background-color: #232629;
            }
            QProgressBar::chunk {
                background-color: #3daee9;
                border-radius: 3px;
            }
        """)
        
        # 创建中央部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        main_layout = QVBoxLayout(central_widget)
        main_layout.setSpacing(10)
        main_layout.setContentsMargins(10, 10, 10, 10)
        
        # 创建标签页
        tab_widget = QTabWidget()
        tab_widget.setFont(QFont("Arial", 10))
        main_layout.addWidget(tab_widget)
        
        # 音乐控制标签页
        music_tab = self.create_music_tab()
        tab_widget.addTab(music_tab, "🎵 音乐控制")
        
        # 舞蹈展示标签页
        dance_tab = self.create_dance_tab()
        tab_widget.addTab(dance_tab, "💃 舞蹈展示")
        
        # 序列编辑标签页
        sequence_tab = self.create_sequence_tab()
        tab_widget.addTab(sequence_tab, "📝 序列编辑")
        
        # 实时监控标签页
        monitor_tab = self.create_monitor_tab()
        tab_widget.addTab(monitor_tab, "📊 实时监控")
        
        # 参数设置标签页
        settings_tab = self.create_settings_tab()
        tab_widget.addTab(settings_tab, "⚙️ 参数设置")
        
        # 状态栏
        self.status_bar = self.statusBar()
        self.status_bar.setStyleSheet("background-color: #31363b; color: #eff0f1;")
        self.status_label = QLabel("系统就绪")
        self.status_bar.addWidget(self.status_label)
        
    def create_music_tab(self):
        """创建音乐控制标签页"""
        tab = QWidget()
        layout = QVBoxLayout(tab)
        layout.setSpacing(15)
        
        # 音乐文件组
        file_group = QGroupBox("音乐文件")
        file_layout = QHBoxLayout()
        file_layout.setSpacing(10)
        self.music_file_edit = QLineEdit()
        self.music_file_edit.setPlaceholderText("请选择音乐文件...")
        self.browse_music_btn = QPushButton("浏览")
        self.browse_music_btn.clicked.connect(self.browse_music_file)
        file_layout.addWidget(QLabel("音乐文件:"))
        file_layout.addWidget(self.music_file_edit)
        file_layout.addWidget(self.browse_music_btn)
        file_group.setLayout(file_layout)
        
        # 控制按钮组
        control_group = QGroupBox("控制")
        control_layout = QHBoxLayout()
        control_layout.setSpacing(15)
        control_layout.setAlignment(Qt.AlignCenter)
        self.play_btn = QPushButton("▶ 播放")
        self.pause_btn = QPushButton("⏸ 暂停")
        self.stop_btn = QPushButton("⏹ 停止")
        self.play_btn.clicked.connect(self.play)
        self.pause_btn.clicked.connect(self.pause)
        self.stop_btn.clicked.connect(self.stop)
        self.pause_btn.setEnabled(False)
        self.stop_btn.setEnabled(False)
        
        # 增大按钮尺寸
        button_font = QFont()
        button_font.setPointSize(12)
        button_font.setBold(True)
        self.play_btn.setFont(button_font)
        self.pause_btn.setFont(button_font)
        self.stop_btn.setFont(button_font)
        
        self.play_btn.setFixedSize(120, 40)
        self.pause_btn.setFixedSize(120, 40)
        self.stop_btn.setFixedSize(120, 40)
        
        control_layout.addWidget(self.play_btn)
        control_layout.addWidget(self.pause_btn)
        control_layout.addWidget(self.stop_btn)
        control_group.setLayout(control_layout)
        
        # 音量控制组
        volume_group = QGroupBox("音量控制")
        volume_layout = QHBoxLayout()
        volume_layout.setSpacing(10)
        self.volume_slider = QProgressBar()
        self.volume_slider.setRange(0, 100)
        self.volume_slider.setValue(50)
        self.volume_slider.setFixedHeight(25)
        volume_layout.addWidget(QLabel("音量:"))
        volume_layout.addWidget(self.volume_slider)
        volume_group.setLayout(volume_layout)
        
        # 添加组件到布局
        layout.addWidget(file_group)
        layout.addWidget(control_group)
        layout.addWidget(volume_group)
        layout.addStretch()
        
        return tab
        
    def create_dance_tab(self):
        """创建舞蹈展示标签页"""
        tab = QWidget()
        layout = QVBoxLayout(tab)
        layout.setSpacing(15)
        
        # 机器人模型展示区域（使用图片代替3D模型）
        model_group = QGroupBox("机器人模型展示")
        model_layout = QVBoxLayout()
        self.model_display = QLabel()
        self.model_display.setAlignment(Qt.AlignCenter)
        self.model_display.setMinimumHeight(300)
        self.model_display.setStyleSheet("""
            background-color: #232629; 
            border: 2px dashed #3daee9; 
            border-radius: 8px;
            color: #aaaaaa;
            font-size: 16px;
        """)
        
        # 设置默认图片占位符
        self.model_display.setText("机器人模型展示区域\n(请添加机器人图片到robot_images文件夹)")
        self.model_display.setScaledContents(False)
        self.model_display.setAlignment(Qt.AlignCenter)
        
        model_layout.addWidget(self.model_display)
        model_group.setLayout(model_layout)
        
        # 动作预览区域
        preview_group = QGroupBox("动作预览")
        preview_layout = QVBoxLayout()
        self.action_preview = QTextEdit()
        self.action_preview.setReadOnly(True)
        self.action_preview.setPlaceholderText("实时动作预览将显示在这里...")
        preview_layout.addWidget(self.action_preview)
        
        # 添加手动连接按钮
        connection_layout = QHBoxLayout()
        self.connect_btn = QPushButton("🔌 连接机器人")
        self.connect_btn.clicked.connect(self.connect_robot)
        self.disconnect_btn = QPushButton("🚫 断开连接")
        self.disconnect_btn.clicked.connect(self.disconnect_robot)
        self.disconnect_btn.setEnabled(False)
        connection_layout.addWidget(self.connect_btn)
        connection_layout.addWidget(self.disconnect_btn)
        preview_layout.addLayout(connection_layout)
        
        preview_group.setLayout(preview_layout)
        
        layout.addWidget(model_group)
        layout.addWidget(preview_group)
        
        return tab
        
    def connect_robot(self):
        """手动连接机器人"""
        # 从配置中获取机器人地址和端口
        robot_address = self.controller.config_manager.get('robot.address', '192.168.1.100')
        robot_port = self.controller.config_manager.get('robot.port', 8080)
        connection_type = self.controller.config_manager.get('robot.connection_type', 'tcp')
        
        address = f"{robot_address}:{robot_port}" if connection_type == 'tcp' else robot_address
        
        # 尝试连接
        success = self.controller.robot_comm.connect(address)
        
        if success:
            self.status_label.setText(f"机器人连接成功: {address}")
            self.connect_btn.setEnabled(False)
            self.disconnect_btn.setEnabled(True)
            QMessageBox.information(self, "连接成功", f"成功连接到机器人: {address}")
        else:
            self.status_label.setText(f"机器人连接失败: {address}")
            QMessageBox.critical(self, "连接失败", f"无法连接到机器人: {address}")
            
    def disconnect_robot(self):
        """手动断开机器人连接"""
        self.controller.robot_comm.disconnect()
        self.status_label.setText("机器人已断开连接")
        self.connect_btn.setEnabled(True)
        self.disconnect_btn.setEnabled(False)
        QMessageBox.information(self, "断开连接", "已断开与机器人的连接")
        
    def create_sequence_tab(self):
        """创建序列编辑标签页"""
        tab = QWidget()
        layout = QVBoxLayout(tab)
        layout.setSpacing(15)
        
        # 时间轴编辑区域
        timeline_group = QGroupBox("🕒 时间轴编辑")
        timeline_layout = QVBoxLayout()
        
        # 导入时间轴编辑器
        try:
            from timeline_editor import TimelineEditorWidget
            self.timeline_editor = TimelineEditorWidget()
            # 连接动作变化信号
            self.timeline_editor.actions_changed.connect(self.update_action_editor)
        except ImportError:
            # 如果时间轴编辑器不可用，使用占位符
            self.timeline_editor = QLabel("时间轴编辑区域\n(功能开发中)")
            self.timeline_editor.setAlignment(Qt.AlignCenter)
            self.timeline_editor.setMinimumHeight(200)
            self.timeline_editor.setStyleSheet("""
                background-color: #232629; 
                border: 2px dashed #3daee9; 
                border-radius: 8px;
                color: #aaaaaa;
                font-size: 16px;
            """)
            
        timeline_layout.addWidget(self.timeline_editor)
        timeline_group.setLayout(timeline_layout)
        
        # 动作调整区域
        adjustment_group = QGroupBox("🔧 动作调整")
        adjustment_layout = QVBoxLayout()
        
        # 创建自定义的文本编辑器以处理焦点事件
        class ActionEditor(QTextEdit):
            def __init__(self, parent=None):
                super().__init__(parent)
                self.main_ui = None
                
            def focusOutEvent(self, event):
                # 当失去焦点时，更新时间轴
                if self.main_ui:
                    self.main_ui.update_timeline_from_text()
                super().focusOutEvent(event)
        
        self.action_editor = ActionEditor()
        self.action_editor.main_ui = self
        self.action_editor.setPlaceholderText("在此编辑和调整舞蹈动作序列...\n修改后点击外部区域可同步到时间轴")
        # 连接文本变化信号
        self.action_editor.textChanged.connect(self.on_action_text_changed)
        adjustment_layout.addWidget(self.action_editor)
        adjustment_group.setLayout(adjustment_layout)
        
        layout.addWidget(timeline_group)
        layout.addWidget(adjustment_group)
        
        return tab
        
    def create_monitor_tab(self):
        """创建实时监控标签页"""
        tab = QWidget()
        layout = QVBoxLayout(tab)
        layout.setSpacing(15)
        
        # 音频波形监控
        waveform_group = QGroupBox("音频波形")
        waveform_layout = QVBoxLayout()
        self.waveform_display = QLabel("音频波形显示区域")
        self.waveform_display.setAlignment(Qt.AlignCenter)
        self.waveform_display.setMinimumHeight(150)
        self.waveform_display.setStyleSheet("""
            background-color: #232629; 
            border: 2px dashed #3daee9; 
            border-radius: 8px;
            color: #aaaaaa;
            font-size: 16px;
        """)
        waveform_layout.addWidget(self.waveform_display)
        waveform_group.setLayout(waveform_layout)
        
        # 系统状态监控
        system_group = QGroupBox("系统状态")
        system_layout = QVBoxLayout()
        self.system_status_display = QTextEdit()
        self.system_status_display.setReadOnly(True)
        self.system_status_display.setMaximumHeight(200)
        system_layout.addWidget(self.system_status_display)
        system_group.setLayout(system_layout)
        
        # 连接状态监控
        connection_group = QGroupBox("连接状态")
        connection_layout = QVBoxLayout()
        self.connection_status = QLabel("连接状态: 未连接")
        self.connection_status.setStyleSheet("font-size: 14px; font-weight: bold; color: #ff6b6b;")
        connection_layout.addWidget(self.connection_status)
        connection_group.setLayout(connection_layout)
        
        layout.addWidget(waveform_group)
        layout.addWidget(system_group)
        layout.addWidget(connection_group)
        
        return tab
        
    def create_settings_tab(self):
        """创建参数设置标签页"""
        tab = QWidget()
        layout = QVBoxLayout(tab)
        layout.setSpacing(15)
        
        # 主题选择组
        theme_group = QGroupBox("主题设置")
        theme_layout = QHBoxLayout()
        theme_layout.setSpacing(10)
        self.theme_combo = QComboBox()
        self.theme_combo.addItem("深色主题", "dark")
        self.theme_combo.addItem("浅色主题", "light")
        self.theme_combo.addItem("蓝色主题", "blue")
        self.theme_combo.currentIndexChanged.connect(self.change_theme)
        theme_layout.addWidget(QLabel("界面主题:"))
        theme_layout.addWidget(self.theme_combo)
        theme_group.setLayout(theme_layout)
        
        # 模式选择组
        mode_group = QGroupBox("模式选择")
        mode_layout = QHBoxLayout()
        mode_layout.setSpacing(10)
        self.mode_combo = QComboBox()
        self.mode_combo.addItem("⚡ 实时模式", DanceMode.REALTIME)
        self.mode_combo.addItem("📋 预设模式", DanceMode.PRESET)
        self.mode_combo.addItem("⏺ 录制模式", DanceMode.RECORDING)
        self.mode_combo.addItem("✏ 编辑模式", DanceMode.EDITING)
        self.mode_combo.currentIndexChanged.connect(self.change_mode)
        mode_layout.addWidget(QLabel("舞蹈模式:"))
        mode_layout.addWidget(self.mode_combo)
        mode_group.setLayout(mode_layout)
        
        # 灵敏度调整组
        sensitivity_group = QGroupBox("灵敏度调整")
        sensitivity_layout = QVBoxLayout()
        sensitivity_layout.setSpacing(10)
        self.sensitivity_spin = QDoubleSpinBox()
        self.sensitivity_spin.setRange(0.1, 5.0)
        self.sensitivity_spin.setSingleStep(0.1)
        self.sensitivity_spin.setValue(1.5)
        sensitivity_layout.addWidget(QLabel("节拍检测灵敏度:"))
        sensitivity_layout.addWidget(self.sensitivity_spin)
        sensitivity_group.setLayout(sensitivity_layout)
        
        # 机器人配置组
        robot_group = QGroupBox("机器人配置")
        robot_layout = QVBoxLayout()
        robot_layout.setSpacing(10)
        self.robot_address_edit = QLineEdit("192.168.1.100")
        self.robot_port_spin = QSpinBox()
        self.robot_port_spin.setRange(1, 65535)
        self.robot_port_spin.setValue(8080)
        robot_layout.addWidget(QLabel("机器人地址:"))
        robot_layout.addWidget(self.robot_address_edit)
        robot_layout.addWidget(QLabel("端口:"))
        robot_layout.addWidget(self.robot_port_spin)
        robot_group.setLayout(robot_layout)
        
        # 添加组件到布局
        layout.addWidget(theme_group)
        layout.addWidget(mode_group)
        layout.addWidget(sensitivity_group)
        layout.addWidget(robot_group)
        layout.addStretch()
        
        return tab
        
    def init_timers(self):
        """初始化定时器"""
        # 界面更新定时器
        self.update_timer = QTimer()
        self.update_timer.timeout.connect(self.update_ui)
        self.update_timer.start(100)  # 每100ms更新一次界面
        
    def initialize_system(self):
        """初始化系统"""
        self.status_label.setText("正在初始化系统...")
        QApplication.processEvents()
        
        # 在后台线程中初始化系统
        init_thread = threading.Thread(target=self._initialize_system_background)
        init_thread.daemon = True
        init_thread.start()
        
    def _initialize_system_background(self):
        """在后台线程中初始化系统"""
        success = self.controller.initialize_system()
        
        # 在主线程中更新界面
        if success:
            self.status_label.setText("系统就绪")
        else:
            self.status_label.setText("系统初始化失败")
            
    def browse_music_file(self):
        """浏览音乐文件"""
        filename, _ = QFileDialog.getOpenFileName(
            self, "选择音乐文件", "", "音频文件 (*.wav *.mp3 *.flac)")
        if filename:
            self.music_file_edit.setText(filename)
            
    def play(self):
        """播放"""
        mode = self.mode_combo.currentData()
        
        if mode == DanceMode.REALTIME:
            self.controller.start_realtime_mode()
        elif mode == DanceMode.PRESET:
            self.controller.start_preset_mode()
        elif mode == DanceMode.RECORDING:
            # 录制模式处理
            self.controller.start_recording_mode()
        elif mode == DanceMode.EDITING:
            # 在编辑模式下，将时间轴数据传递给动作生成器
            try:
                from timeline_editor import TimelineEditorWidget
                if isinstance(self.timeline_editor, TimelineEditorWidget):
                    actions_data = self.timeline_editor.get_actions_data()
                    print(f"从时间轴获取动作数据，数量: {len(actions_data)}")
                    if actions_data:
                        self.controller.motion_generator.set_editing_sequence(actions_data)
                    else:
                        QMessageBox.warning(self, "警告", "时间轴中没有动作序列，请先添加动作")
                        return
            except Exception as e:
                print(f"获取时间轴数据失败: {e}")
                QMessageBox.critical(self, "错误", f"无法获取时间轴数据: {e}")
                return
            self.controller.start_editing_mode()
            
        self.play_btn.setEnabled(False)
        self.pause_btn.setEnabled(True)
        self.stop_btn.setEnabled(True)

    def pause(self):
        """暂停"""
        self.controller.pause()
        self.play_btn.setEnabled(True)
        self.pause_btn.setEnabled(False)
        
    def stop(self):
        """停止"""
        self.controller.stop()
        self.play_btn.setEnabled(True)
        self.pause_btn.setEnabled(False)
        self.stop_btn.setEnabled(False)
        
    def update_ui(self):
        """更新界面"""
        if self.updating:
            return
            
        self.updating = True
        
        try:
            # 更新系统状态显示
            status = self.controller.get_system_status()
            status_text = f"系统状态: {status['state']}\n"
            status_text += f"舞蹈模式: {status['mode']}\n"
            status_text += f"机器人状态: {status['robot_status']['state']}\n"
            status_text += f"总节拍数: {status['audio_stats']['total_beats']}\n"
            status_text += f"BPM: {status['audio_stats']['bpm']:.1f}"
            
            self.system_status_display.setText(status_text)
            
            # 更新连接状态
            robot_status = status['robot_status']
            if robot_status['state'] == '已连接':
                self.connection_status.setText(f"连接状态: {robot_status['state']}")
                self.connection_status.setStyleSheet("font-size: 14px; font-weight: bold; color: #2ecc71;")
            else:
                self.connection_status.setText(f"连接状态: {robot_status['state']}")
                self.connection_status.setStyleSheet("font-size: 14px; font-weight: bold; color: #ff6b6b;")
            
            # 根据系统状态更新按钮状态
            current_state = self.controller.state_manager.get_current_state()
            if current_state == SystemState.PLAYING:
                self.play_btn.setEnabled(False)
                self.pause_btn.setEnabled(True)
                self.stop_btn.setEnabled(True)
            elif current_state == SystemState.PAUSED:
                self.play_btn.setEnabled(True)
                self.pause_btn.setEnabled(False)
                self.stop_btn.setEnabled(True)
            elif current_state in [SystemState.READY, SystemState.IDLE]:
                self.play_btn.setEnabled(True)
                self.pause_btn.setEnabled(False)
                self.stop_btn.setEnabled(False)
                
        except Exception as e:
            print(f"界面更新错误: {e}")
        finally:
            self.updating = False
            
    def update_robot_model_display(self, action=None):
        """
        更新机器人模型展示
        
        Args:
            action: 机器人动作对象，如果为None则显示默认图片
        """
        if hasattr(self, 'robot_model_viewer'):
            self.robot_model_viewer.update_model_display(action)
            
    def update_action_editor(self):
        """更新动作编辑器内容"""
        try:
            from timeline_editor import TimelineEditorWidget
            if isinstance(self.timeline_editor, TimelineEditorWidget):
                actions_data = self.timeline_editor.get_actions_data()
                import json
                actions_text = json.dumps(actions_data, indent=2, ensure_ascii=False)
                # 避免触发textChanged信号导致循环更新
                self.action_editor.blockSignals(True)
                self.action_editor.setPlainText(actions_text)
                self.action_editor.blockSignals(False)
        except Exception as e:
            print(f"更新动作编辑器时出错: {e}")
            
    def on_action_text_changed(self):
        """处理动作文本变化"""
        # 不再需要处理，改为在失去焦点时处理
        pass
    
    def update_timeline_from_text(self):
        """从动作编辑器文本更新时间轴"""
        try:
            from timeline_editor import TimelineEditorWidget
            if isinstance(self.timeline_editor, TimelineEditorWidget):
                text = self.action_editor.toPlainText()
                if text.strip():
                    self.timeline_editor.update_from_json(text)
        except Exception as e:
            print(f"从文本更新时间轴时出错: {e}")

    def change_theme(self, index):
        """切换主题"""
        theme = self.theme_combo.currentData()
        
        if theme == "dark":
            self.setStyleSheet("""
                QMainWindow {
                    background-color: #2b2b2b;
                }
                QGroupBox {
                    font-weight: bold;
                    border: 2px solid #3daee9;
                    border-radius: 8px;
                    margin-top: 1ex;
                    padding-top: 10px;
                    background-color: #31363b;
                    color: #eff0f1;
                }
                QGroupBox::title {
                    subcontrol-origin: margin;
                    subcontrol-position: top center;
                    padding: 0 5px;
                    background-color: #3daee9;
                    color: white;
                    border-radius: 3px;
                }
                QLabel {
                    color: #eff0f1;
                }
                QPushButton {
                    background-color: #3daee9;
                    border: none;
                    color: white;
                    padding: 8px 16px;
                    border-radius: 4px;
                    font-weight: bold;
                }
                QPushButton:hover {
                    background-color: #4db8ff;
                }
                QPushButton:pressed {
                    background-color: #2e86b3;
                }
                QPushButton:disabled {
                    background-color: #7a7a7a;
                    color: #aaaaaa;
                }
                QTabWidget::pane {
                    border: 1px solid #3daee9;
                    border-radius: 5px;
                    background-color: #31363b;
                }
                QTabBar::tab {
                    background-color: #232629;
                    color: #aaaaaa;
                    padding: 10px;
                    border: 1px solid #3daee9;
                    border-bottom: none;
                    border-top-left-radius: 5px;
                    border-top-right-radius: 5px;
                }
                QTabBar::tab:selected {
                    background-color: #3daee9;
                    color: white;
                    font-weight: bold;
                }
                QTabBar::tab:hover:!selected {
                    background-color: #336699;
                    color: white;
                }
                QLineEdit {
                    padding: 5px;
                    border: 1px solid #3daee9;
                    border-radius: 4px;
                    background-color: #232629;
                    color: #eff0f1;
                }
                QComboBox {
                    padding: 5px;
                    border: 1px solid #3daee9;
                    border-radius: 4px;
                    background-color: #232629;
                    color: #eff0f1;
                }
                QComboBox::drop-down {
                    border: none;
                }
                QSpinBox, QDoubleSpinBox {
                    padding: 5px;
                    border: 1px solid #3daee9;
                    border-radius: 4px;
                    background-color: #232629;
                    color: #eff0f1;
                }
                QTextEdit {
                    border: 1px solid #3daee9;
                    border-radius: 4px;
                    background-color: #232629;
                    color: #eff0f1;
                }
                QProgressBar {
                    border: 1px solid #3daee9;
                    border-radius: 4px;
                    text-align: center;
                    background-color: #232629;
                }
                QProgressBar::chunk {
                    background-color: #3daee9;
                    border-radius: 3px;
                }
            """)
        elif theme == "light":
            self.setStyleSheet("""
                QMainWindow {
                    background-color: #f0f0f0;
                }
                QGroupBox {
                    font-weight: bold;
                    border: 2px solid #4a90e2;
                    border-radius: 8px;
                    margin-top: 1ex;
                    padding-top: 10px;
                    background-color: #ffffff;
                    color: #333333;
                }
                QGroupBox::title {
                    subcontrol-origin: margin;
                    subcontrol-position: top center;
                    padding: 0 5px;
                    background-color: #4a90e2;
                    color: white;
                    border-radius: 3px;
                }
                QLabel {
                    color: #333333;
                }
                QPushButton {
                    background-color: #4a90e2;
                    border: none;
                    color: white;
                    padding: 8px 16px;
                    border-radius: 4px;
                    font-weight: bold;
                }
                QPushButton:hover {
                    background-color: #5aa0f0;
                }
                QPushButton:pressed {
                    background-color: #3a70c2;
                }
                QPushButton:disabled {
                    background-color: #cccccc;
                    color: #888888;
                }
                QTabWidget::pane {
                    border: 1px solid #4a90e2;
                    border-radius: 5px;
                    background-color: #ffffff;
                }
                QTabBar::tab {
                    background-color: #e0e0e0;
                    color: #666666;
                    padding: 10px;
                    border: 1px solid #4a90e2;
                    border-bottom: none;
                    border-top-left-radius: 5px;
                    border-top-right-radius: 5px;
                }
                QTabBar::tab:selected {
                    background-color: #4a90e2;
                    color: white;
                    font-weight: bold;
                }
                QTabBar::tab:hover:!selected {
                    background-color: #a0c0f0;
                    color: white;
                }
                QLineEdit {
                    padding: 5px;
                    border: 1px solid #4a90e2;
                    border-radius: 4px;
                    background-color: #ffffff;
                    color: #333333;
                }
                QComboBox {
                    padding: 5px;
                    border: 1px solid #4a90e2;
                    border-radius: 4px;
                    background-color: #ffffff;
                    color: #333333;
                }
                QComboBox::drop-down {
                    border: none;
                }
                QSpinBox, QDoubleSpinBox {
                    padding: 5px;
                    border: 1px solid #4a90e2;
                    border-radius: 4px;
                    background-color: #ffffff;
                    color: #333333;
                }
                QTextEdit {
                    border: 1px solid #4a90e2;
                    border-radius: 4px;
                    background-color: #ffffff;
                    color: #333333;
                }
                QProgressBar {
                    border: 1px solid #4a90e2;
                    border-radius: 4px;
                    text-align: center;
                    background-color: #ffffff;
                }
                QProgressBar::chunk {
                    background-color: #4a90e2;
                    border-radius: 3px;
                }
            """)
        elif theme == "blue":
            self.setStyleSheet("""
                QMainWindow {
                    background-color: #1e3c72;
                }
                QGroupBox {
                    font-weight: bold;
                    border: 2px solid #2a5298;
                    border-radius: 8px;
                    margin-top: 1ex;
                    padding-top: 10px;
                    background-color: #2a5298;
                    color: #ffffff;
                }
                QGroupBox::title {
                    subcontrol-origin: margin;
                    subcontrol-position: top center;
                    padding: 0 5px;
                    background-color: #3a6bc8;
                    color: white;
                    border-radius: 3px;
                }
                QLabel {
                    color: #ffffff;
                }
                QPushButton {
                    background-color: #3a6bc8;
                    border: none;
                    color: white;
                    padding: 8px 16px;
                    border-radius: 4px;
                    font-weight: bold;
                }
                QPushButton:hover {
                    background-color: #4a7bd8;
                }
                QPushButton:pressed {
                    background-color: #2a5bb8;
                }
                QPushButton:disabled {
                    background-color: #5a7ac8;
                    color: #aaaaaa;
                }
                QTabWidget::pane {
                    border: 1px solid #2a5298;
                    border-radius: 5px;
                    background-color: #2a5298;
                }
                QTabBar::tab {
                    background-color: #1e3c72;
                    color: #cccccc;
                    padding: 10px;
                    border: 1px solid #2a5298;
                    border-bottom: none;
                    border-top-left-radius: 5px;
                    border-top-right-radius: 5px;
                }
                QTabBar::tab:selected {
                    background-color: #3a6bc8;
                    color: white;
                    font-weight: bold;
                }
                QTabBar::tab:hover:!selected {
                    background-color: #2a5bb8;
                    color: white;
                }
                QLineEdit {
                    padding: 5px;
                    border: 1px solid #3a6bc8;
                    border-radius: 4px;
                    background-color: #1e3c72;
                    color: #ffffff;
                }
                QComboBox {
                    padding: 5px;
                    border: 1px solid #3a6bc8;
                    border-radius: 4px;
                    background-color: #1e3c72;
                    color: #ffffff;
                }
                QComboBox::drop-down {
                    border: none;
                }
                QSpinBox, QDoubleSpinBox {
                    padding: 5px;
                    border: 1px solid #3a6bc8;
                    border-radius: 4px;
                    background-color: #1e3c72;
                    color: #ffffff;
                }
                QTextEdit {
                    border: 1px solid #3a6bc8;
                    border-radius: 4px;
                    background-color: #1e3c72;
                    color: #ffffff;
                }
                QProgressBar {
                    border: 1px solid #3a6bc8;
                    border-radius: 4px;
                    text-align: center;
                    background-color: #1e3c72;
                }
                QProgressBar::chunk {
                    background-color: #3a6bc8;
                    border-radius: 3px;
                }
            """)
            
    def change_mode(self, index):
        """切换舞蹈模式"""
        # 获取当前选择的模式
        mode = self.mode_combo.currentData()
        # 可以在这里添加模式切换时的特殊处理逻辑
        print(f"切换到模式: {mode}")

def main():
    """主函数"""
    app = QApplication(sys.argv)
    ui = MainUI()
    ui.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()